home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 March / maximum-cd-2000-03.iso / Quake3 Game Source / Q3AGameSource.exe / Main / ui_cdkey.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-18  |  6.5 KB  |  272 lines

  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3. /*
  4. =======================================================================
  5.  
  6. CD KEY MENU
  7.  
  8. =======================================================================
  9. */
  10.  
  11.  
  12. #include "ui_local.h"
  13.  
  14.  
  15. #define ART_FRAME        "menu/art/cut_frame"
  16. #define ART_ACCEPT0        "menu/art/accept_0"
  17. #define ART_ACCEPT1        "menu/art/accept_1"    
  18. #define ART_BACK0        "menu/art/back_0"
  19. #define ART_BACK1        "menu/art/back_1"    
  20.  
  21. #define ID_CDKEY        10
  22. #define ID_ACCEPT        11
  23. #define ID_BACK            12
  24.  
  25.  
  26. typedef struct {
  27.     menuframework_s    menu;
  28.  
  29.     menutext_s        banner;
  30.     menubitmap_s    frame;
  31.  
  32.     menufield_s        cdkey;
  33.  
  34.     menubitmap_s    accept;
  35.     menubitmap_s    back;
  36. } cdkeyMenuInfo_t;
  37.  
  38. static cdkeyMenuInfo_t    cdkeyMenuInfo;
  39.  
  40.  
  41. /*
  42. ===============
  43. UI_CDKeyMenu_Event
  44. ===============
  45. */
  46. static void UI_CDKeyMenu_Event( void *ptr, int event ) {
  47.     if( event != QM_ACTIVATED ) {
  48.         return;
  49.     }
  50.  
  51.     switch( ((menucommon_s*)ptr)->id ) {
  52.     case ID_ACCEPT:
  53.         if( cdkeyMenuInfo.cdkey.field.buffer[0] ) {
  54.             trap_SetCDKey( cdkeyMenuInfo.cdkey.field.buffer );
  55.         }
  56.         UI_PopMenu();
  57.         break;
  58.  
  59.     case ID_BACK:
  60.         UI_PopMenu();
  61.         break;
  62.     }
  63. }
  64.  
  65.  
  66. /*
  67. =================
  68. UI_CDKeyMenu_PreValidateKey
  69. =================
  70. */
  71. static int UI_CDKeyMenu_PreValidateKey( const char *key ) {
  72.     char    ch;
  73.  
  74.     if( strlen( key ) != 16 ) {
  75.         return 1;
  76.     }
  77.  
  78.     while( ( ch = *key++ ) ) {
  79.         switch( ch ) {
  80.         case '2':
  81.         case '3':
  82.         case '7':
  83.         case 'a':
  84.         case 'b':
  85.         case 'c':
  86.         case 'd':
  87.         case 'g':
  88.         case 'h':
  89.         case 'j':
  90.         case 'l':
  91.         case 'p':
  92.         case 'r':
  93.         case 's':
  94.         case 't':
  95.         case 'w':
  96.             continue;
  97.         default:
  98.             return -1;
  99.         }
  100.     }
  101.  
  102.     return 0;
  103. }
  104.  
  105.  
  106. /*
  107. =================
  108. UI_CDKeyMenu_DrawKey
  109. =================
  110. */
  111. static void UI_CDKeyMenu_DrawKey( void *self ) {
  112.     menufield_s        *f;
  113.     qboolean        focus;
  114.     int                style;
  115.     char            c;
  116.     float            *color;
  117.     int                x, y;
  118.     int                val;
  119.  
  120.     f = (menufield_s *)self;
  121.  
  122.     focus = (f->generic.parent->cursor == f->generic.menuPosition);
  123.  
  124.     style = UI_LEFT;
  125.     if( focus ) {
  126.         color = color_yellow;
  127.     }
  128.     else {
  129.         color = color_orange;
  130.     }
  131.  
  132.     x = 320 - 8 * BIGCHAR_WIDTH;
  133.     y = 240 - BIGCHAR_HEIGHT / 2;
  134.     UI_FillRect( x, y, 16 * BIGCHAR_WIDTH, BIGCHAR_HEIGHT, listbar_color );
  135.     UI_DrawString( x, y, f->field.buffer, style, color );
  136.  
  137.     // draw cursor if we have focus
  138.     if( focus ) {
  139.         if ( trap_Key_GetOverstrikeMode() ) {
  140.             c = 11;
  141.         } else {
  142.             c = 10;
  143.         }
  144.  
  145.         style &= ~UI_PULSE;
  146.         style |= UI_BLINK;
  147.  
  148.         UI_DrawChar( x + f->field.cursor * BIGCHAR_WIDTH, y, c, style, color_white );
  149.     }
  150.  
  151.     val = UI_CDKeyMenu_PreValidateKey( f->field.buffer );
  152.     if( val == 1 ) {
  153.         UI_DrawProportionalString( 320, 376, "Please enter your CD Key", UI_CENTER|UI_SMALLFONT, color_yellow );
  154.     }
  155.     else if ( val == 0 ) {
  156.         UI_DrawProportionalString( 320, 376, "The CD Key appears to be valid, thank you", UI_CENTER|UI_SMALLFONT, color_white );
  157.     }
  158.     else {
  159.         UI_DrawProportionalString( 320, 376, "The CD Key is not valid", UI_CENTER|UI_SMALLFONT, color_red );
  160.     }
  161. }
  162.  
  163.  
  164. /*
  165. ===============
  166. UI_CDKeyMenu_Init
  167. ===============
  168. */
  169. static void UI_CDKeyMenu_Init( void ) {
  170.     trap_Cvar_Set( "ui_cdkeychecked", "1" );
  171.  
  172.     UI_CDKeyMenu_Cache();
  173.  
  174.     memset( &cdkeyMenuInfo, 0, sizeof(cdkeyMenuInfo) );
  175.     cdkeyMenuInfo.menu.wrapAround = qtrue;
  176.     cdkeyMenuInfo.menu.fullscreen = qtrue;
  177.  
  178.     cdkeyMenuInfo.banner.generic.type                = MTYPE_BTEXT;
  179.     cdkeyMenuInfo.banner.generic.x                    = 320;
  180.     cdkeyMenuInfo.banner.generic.y                    = 16;
  181.     cdkeyMenuInfo.banner.string                        = "CD KEY";
  182.     cdkeyMenuInfo.banner.color                        = color_white;
  183.     cdkeyMenuInfo.banner.style                        = UI_CENTER;
  184.  
  185.     cdkeyMenuInfo.frame.generic.type                = MTYPE_BITMAP;
  186.     cdkeyMenuInfo.frame.generic.name                = ART_FRAME;
  187.     cdkeyMenuInfo.frame.generic.flags                = QMF_INACTIVE;
  188.     cdkeyMenuInfo.frame.generic.x                    = 142;
  189.     cdkeyMenuInfo.frame.generic.y                    = 118;
  190.     cdkeyMenuInfo.frame.width                          = 359;
  191.     cdkeyMenuInfo.frame.height                      = 256;
  192.  
  193.     cdkeyMenuInfo.cdkey.generic.type                = MTYPE_FIELD;
  194.     cdkeyMenuInfo.cdkey.generic.name                = "CD Key:";
  195.     cdkeyMenuInfo.cdkey.generic.flags                = QMF_LOWERCASE;
  196.     cdkeyMenuInfo.cdkey.generic.x                    = 320 - BIGCHAR_WIDTH * 2.5;
  197.     cdkeyMenuInfo.cdkey.generic.y                    = 240 - BIGCHAR_HEIGHT / 2;
  198.     cdkeyMenuInfo.cdkey.field.widthInChars            = 16;
  199.     cdkeyMenuInfo.cdkey.field.maxchars                = 16;
  200.     cdkeyMenuInfo.cdkey.generic.ownerdraw            = UI_CDKeyMenu_DrawKey;
  201.  
  202.     cdkeyMenuInfo.accept.generic.type                = MTYPE_BITMAP;
  203.     cdkeyMenuInfo.accept.generic.name                = ART_ACCEPT0;
  204.     cdkeyMenuInfo.accept.generic.flags                = QMF_RIGHT_JUSTIFY|QMF_PULSEIFFOCUS;
  205.     cdkeyMenuInfo.accept.generic.id                    = ID_ACCEPT;
  206.     cdkeyMenuInfo.accept.generic.callback            = UI_CDKeyMenu_Event;
  207.     cdkeyMenuInfo.accept.generic.x                    = 640;
  208.     cdkeyMenuInfo.accept.generic.y                    = 480-64;
  209.     cdkeyMenuInfo.accept.width                        = 128;
  210.     cdkeyMenuInfo.accept.height                        = 64;
  211.     cdkeyMenuInfo.accept.focuspic                    = ART_ACCEPT1;
  212.  
  213.     cdkeyMenuInfo.back.generic.type                    = MTYPE_BITMAP;
  214.     cdkeyMenuInfo.back.generic.name                    = ART_BACK0;
  215.     cdkeyMenuInfo.back.generic.flags                = QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS;
  216.     cdkeyMenuInfo.back.generic.id                    = ID_BACK;
  217.     cdkeyMenuInfo.back.generic.callback                = UI_CDKeyMenu_Event;
  218.     cdkeyMenuInfo.back.generic.x                    = 0;
  219.     cdkeyMenuInfo.back.generic.y                    = 480-64;
  220.     cdkeyMenuInfo.back.width                        = 128;
  221.     cdkeyMenuInfo.back.height                        = 64;
  222.     cdkeyMenuInfo.back.focuspic                        = ART_BACK1;
  223.  
  224.     Menu_AddItem( &cdkeyMenuInfo.menu, &cdkeyMenuInfo.banner );
  225.     Menu_AddItem( &cdkeyMenuInfo.menu, &cdkeyMenuInfo.frame );
  226.     Menu_AddItem( &cdkeyMenuInfo.menu, &cdkeyMenuInfo.cdkey );
  227.     Menu_AddItem( &cdkeyMenuInfo.menu, &cdkeyMenuInfo.accept );
  228.     if( uis.menusp ) {
  229.         Menu_AddItem( &cdkeyMenuInfo.menu, &cdkeyMenuInfo.back );
  230.     }
  231.  
  232.     trap_GetCDKey( cdkeyMenuInfo.cdkey.field.buffer, cdkeyMenuInfo.cdkey.field.maxchars + 1 );
  233.     if( strcmp( cdkeyMenuInfo.cdkey.field.buffer, "123456789" ) == 0 ) {
  234.         cdkeyMenuInfo.cdkey.field.buffer[0] = 0;
  235.     }
  236. }
  237.  
  238.  
  239. /*
  240. =================
  241. UI_CDKeyMenu_Cache
  242. =================
  243. */
  244. void UI_CDKeyMenu_Cache( void ) {
  245.     trap_R_RegisterShaderNoMip( ART_ACCEPT0 );
  246.     trap_R_RegisterShaderNoMip( ART_ACCEPT1 );
  247.     trap_R_RegisterShaderNoMip( ART_BACK0 );
  248.     trap_R_RegisterShaderNoMip( ART_BACK1 );
  249.     trap_R_RegisterShaderNoMip( ART_FRAME );
  250. }
  251.  
  252.  
  253. /*
  254. ===============
  255. UI_CDKeyMenu
  256. ===============
  257. */
  258. void UI_CDKeyMenu( void ) {
  259.     UI_CDKeyMenu_Init();
  260.     UI_PushMenu( &cdkeyMenuInfo.menu );
  261. }
  262.  
  263.  
  264. /*
  265. ===============
  266. UI_CDKeyMenu_f
  267. ===============
  268. */
  269. void UI_CDKeyMenu_f( void ) {
  270.     UI_CDKeyMenu();
  271. }
  272.